import 'package:aihu_getx_app/app/modules/commons/model/dict_data_model.dart';
import 'package:aihu_getx_app/app/modules/d/d_home/controllers/d_home_controller.dart';
import 'package:aihu_getx_app/app/modules/d/d_member/d_member_vipuser_index/models/vip_user_page_req_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_member/d_member_vipuser_index/models/vip_user_resp_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_member/d_member_vipuser_index/providers/d_member_vipuser_provider.dart';
import 'package:aihu_getx_app/utils/constants.dart';
import 'package:aihu_getx_app/utils/init_system_data_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';

class DMemberVipuserIndexController extends DHomeController {
  final count = 0.obs;
  final statusList = <DictDataModel>[].obs;
  final dataList = <VipUserRespVO>[].obs;
  final scaffoldKey = GlobalKey<ScaffoldState>();
  final searchForm = VipUserPageReqVO().obs;
  final searchFormKey = GlobalKey<FormBuilderState>();
  final searchFormNickNameController = TextEditingController();
  final loading = false.obs;
  final hasMore = true.obs;
  final loadMoreText = "正在加载中...".obs;
  final scrollController = ScrollController();

  Future onRefresh() {
    if (loading.isTrue) {
      return Future.value();
    }
    hasMore.value = true;
    dataList.value = [];
    searchForm.value.pageNo = 1;
    onLoading();
    return Future.value();
  }

  void onLoading() async {
    loading.value = true;
    try {
      var res = await DMemberVipuserProvider.page(searchForm.value);
      dataList.addAll(res.list!);
      if (res.list!.isEmpty) {
        hasMore.value = false;
        loadMoreText.value = "没有更多数据了";
      } else {
        loadMoreText.value = "上拉加载更多";
      }
    } finally {
      loading.value = false;
    }
  }

  @override
  void onInit() {
    super.onInit();
    pageTitle.value = Get.arguments?['pageTitle'] ?? pageTitle.value;
    statusList.value =
        InitSystemDataUtil.getDictListByType(DictTypeEnum.commonStatus.type);
  }

  @override
  void onReady() async {
    super.onReady();
    scrollController.addListener(() {
      if (hasMore.isFalse || loading.isTrue) {
        return;
      }
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        searchForm.value.pageNo++;
        loadMoreText.value = "正在加载中...";
        onLoading();
      }
    });
    await onRefresh();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  Future<bool> updateUser(VipUserRespVO data) {
    return DMemberVipuserProvider.update(data);
  }

  Widget buildProgressMoreIndicator() {
    return Padding(
      padding: const EdgeInsets.all(15.0),
      child: Center(
        child: Obx(
          () => Text(
            loadMoreText.value,
            style: Get.textTheme.bodyMedium,
          ),
        ),
      ),
    );
  }
}
